$(function(){
    template.defaults.imports.dateFormat = function (dateStr) {
        // console.log(dateStr)
        var dt = new Date(dateStr)
        var y = dt.getFullYear()
        var m = padZero(dt.getMonth() + 1)
        var d = padZero(dt.getDate())
    
        var hh = padZero(dt.getHours())
        var mm = padZero(dt.getMinutes())
        var ss = padZero(dt.getSeconds())
    
        // return 'yyyy-mm-dd'
        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
      }
      // 补零函数
      function padZero (n) {
        return n < 10 ? '0' + n : n
      }



var id = location.search.substring(1).split("=")[1];
console.log(id);
// 渲染评论功能
var count = 0;
function getDetail() {
  $.ajax({
    method: "get",
    url: "http://localhost:8888/api/articles/" + id + "/comments",
    success(res) {
      console.log(res);
      if (res.status !== 0) {
        return layui.layer.msg(res.message, { icon: 5 });
      }
      // count = res.data[1].count
      // console.log(count);
      var htmlStr = template("detail_tpl", res);
      $(".kr_comment_card").html(htmlStr);
    },
  });
}
getDetail();
// 发表评论功能
$("body").on("submit", "#comment-form", function (e) {
  e.preventDefault();
  var data = $(this).serialize();
  console.log(data);
  $.ajax({
    method: "post",
    url: "http://localhost:8888/api/articles/" + id + "/comments",
    data,
    success(res) {
      console.log(res);
      if (res.status !== 0) return layui.layer.msg(res.message, { icon: 5 });
      layui.layer.msg(res.message, { icon: 6 }, function () {
        // initCateList();
        // layui.layer.close(indexadd)
        getDetail();
      });
    },
  });
});
//评论字数
$(function () {
  $("#content").keyup(function () {
    var length = $(this).val().length;
    if (length > 199) {
      $(this).val($(this).val().substring(0, 200));
      $(".word_count").text(0);
    }
    var num = 200 - length;
    if (num < 0) {
      $(".word_count").text(0);
    } else {
      $(".word_count").text(num);
    }
  })
})
})
